package com.sinosoft.orderinfo;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.sinosoft.base.Constants;
import com.sinosoft.base.GenericDaoHibernate;
import com.sinosoft.util.DateTimeUtils;
import com.sinosoft.util.SystemLogUtil;

@Repository
public class OrderInfoDaoHibernate extends GenericDaoHibernate<OrderInfo, Long> implements OrderInfoDao{
	
	public OrderInfoDaoHibernate()
	{
		super(OrderInfo.class);
	}
	
	public OrderInfo saveOrderInfo(OrderInfo o)
	{
		getHibernateTemplate().saveOrUpdate(o);
		SystemLogUtil.saveError(Constants.LOG_INFO, "sava order sucess："+o.getOrderid());
		return o;
	}

	@SuppressWarnings("unchecked")
	public List<OrderInfo> searchOrderInfoListByOrder(String hql) {
		List<OrderInfo> orderlist = getHibernateTemplate().find(hql);
		return orderlist;
	}
	
	@SuppressWarnings("unchecked")
	public OrderInfo getOrderInfoById(String orderid)
	{
		String hqls = "from OrderInfo where orderid = "+orderid;
		List<OrderInfo> orderlist = getHibernateTemplate().find(hqls);
		OrderInfo oimodel = null;
		if(orderlist != null && orderlist.size()>0)
			oimodel = orderlist.get(0);
		return oimodel;
	}
	
	public int getOrderInfoCount(String sql)
	{
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Query query = session.createSQLQuery(sql);
		int count = Integer.parseInt(query.list().get(0).toString());
		session.close();
		return count;
	}
	
	public List<OrderInfo> getOrderInfoList(OrderInfo orderinfo,int first,int pagesize)
	{
		Session session =null;
		List<OrderInfo> orderinfolist = null;
		session=this.getHibernateTemplate().getSessionFactory().openSession();
		Criteria c = session.createCriteria(OrderInfo.class);
		c.setFirstResult(first);
		c.setMaxResults(pagesize);
		c.addOrder(Order.desc("orderid"));
		String satelliteid = orderinfo.getSatelliteid();
		String username = orderinfo.getUsername();
		String sensorid = orderinfo.getSensorid();
		BigDecimal areadystate = orderinfo.getAreadystate();
		BigDecimal orderstate = orderinfo.getOrderstate();
		BigDecimal sceneid = orderinfo.getSceneid();
		String productlevel = orderinfo.getProductlevel();
		BigDecimal orderid = orderinfo.getOrderid();
		BigDecimal productid = orderinfo.getProductid();
		String orderstarttime = orderinfo.getOrderstarttime();
		String orderendtime = orderinfo.getOrderendtime();
		String checkstarttime = orderinfo.getCheckstarttime();
		String checkendtime = orderinfo.getCheckendtime();
		if(username!=null&&!username.isEmpty())
		{
			c.add(Restrictions.eq("username", username));
		}
		if(sensorid!=null&&!sensorid.isEmpty())
		{
			c.add(Restrictions.eq("sensorid", sensorid));
		}
		if(areadystate!=null&&!areadystate.equals(""))
		{
			c.add(Restrictions.eq("areadystate", areadystate));
		}
		if(orderstate!=null&&!orderstate.equals(""))
		{
			c.add(Restrictions.eq("orderstate", orderstate));
		}
		if(satelliteid != null && !satelliteid.isEmpty())
		{
			c.add(Restrictions.like("satelliteid", satelliteid));
		}
		if(productlevel != null && !productlevel.isEmpty())
		{
			c.add(Restrictions.eq("productlevel", productlevel));
		}
		if(sceneid != null && !sceneid.equals(""))
		{
			c.add(Restrictions.eq("sceneid", sceneid));
		}
		if(orderid != null && !orderid.equals(""))
		{
			c.add(Restrictions.eq("orderid", orderid));
		}
		if(productid != null && !productid.equals(""))
		{
			c.add(Restrictions.eq("productid", productid));
		}
		if(orderstarttime != null && !orderstarttime.isEmpty())
		{
			c.add(Restrictions.gt("tasktime", DateTimeUtils.StringToDate(orderstarttime,"yyyy-MM-dd")));
		}
		if(orderendtime != null && !orderendtime.isEmpty())
		{
			c.add(Restrictions.lt("tasktime", DateTimeUtils.StringToDate(orderendtime,"yyyy-MM-dd")));
		}
		if(checkstarttime != null && !checkstarttime.isEmpty())
		{
			c.add(Restrictions.gt("checktime", DateTimeUtils.StringToDate(checkstarttime,"yyyy-MM-dd")));
		}
		if(checkendtime != null && !checkendtime.isEmpty())
		{
			c.add(Restrictions.lt("checktime", DateTimeUtils.StringToDate(checkendtime,"yyyy-MM-dd")));
		}
		orderinfolist = c.list();
		session.close();
		return orderinfolist;
	}
	
	public List<OrderInfo> getOrderListByIds(String sqlstr)
	{
		List<OrderInfo> orderlist = getHibernateTemplate().find(sqlstr);
		return orderlist;
	}
	
	public List<OrderInfo> getOrderinfoListByUsername(String username,String orderids)
	{
		StringBuffer hql = new StringBuffer("from OrderInfo where username='"+username+"'");
		if(orderids!=null && !orderids.isEmpty())
		{
			String[] orderlist = orderids.split(",");
			hql.append(" and (");
			for (int i = 0; i < orderlist.length; i++) {
				if(i==0)
					hql.append(" orderid="+orderlist[i]);
				else
					hql.append(" or orderid="+orderlist[i]);
			}
			hql.append(")");
		}
		List<OrderInfo> orderlist = getHibernateTemplate().find(hql.toString());
		return orderlist;
	}
	
	public void removeOrderinfo(Long orderid) {
		String id = orderid.toString();
		// TODO Auto-generated method stub
		OrderInfo orderInfo = new OrderInfo();
		List<OrderInfo> orderlist  = getHibernateTemplate().find("from OrderInfo where orderid="+id);
		if(orderlist.size()>0)
			orderInfo = orderlist.get(0);
		getHibernateTemplate().delete(orderInfo);
	}
}
